package com.bdjk.bdmw.InfoGath.communication.protocol;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Pattern;

import com.bdjk.bdmw.InfoGath.business.beianbei.service.impl.BeiAnBeiPushServiceImpl;
import com.bdjk.bdmw.InfoGath.business.beianbei.service.impl.BeiAnBeiPositionServiceImpl;
import com.bdjk.bdmw.InfoGath.business.heremi.service.impl.HereMiPositionServiceImpl;
import com.bdjk.bdmw.InfoGath.business.iparent.service.impl.IParentPositionServiceImpl;
import com.bdjk.bdmw.InfoGath.common.constants.ProtocolConstants;
import com.bdjk.bdmw.InfoGath.common.service.BaseService;
import com.bdjk.bdmw.InfoGath.communication.protocol.beianbei.BeiAnBeiAlarmProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.beianbei.BeiAnBeiLinkProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.beianbei.BeiAnBeiPositionProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.beianbei.BeiAnBeiRecordProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.heremi.HereMiPositionProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.iparent.IParentGPSProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.littleNurse.NurseGPSProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.littleNurse.NurseLBSProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.littleNurse.NurseLinkProtocol;
import com.bdjk.bdmw.InfoGath.communication.protocol.littleNurse.NurseMultiLBSProtocol;

public class ProtocolPool {
	
	private static Map<String,BaseProtocol> protocolPool = new HashMap<String,BaseProtocol>();
	private Map<String,BaseService> servicePool=new HashMap<String,BaseService>();
	
	static{
		protocolPool.put(ProtocolConstants.HEREMI_POSITION_PATTERN, new HereMiPositionProtocol());
		protocolPool.put(ProtocolConstants.IPARENT_GPS_PATTERN, new IParentGPSProtocol());
		protocolPool.put(ProtocolConstants.BEIANBEI_LINK_PATTERN, new BeiAnBeiLinkProtocol());
		protocolPool.put(ProtocolConstants.BEIANBEI_POSITION_PATTERN, new BeiAnBeiPositionProtocol());
		protocolPool.put(ProtocolConstants.BEIANBEI_ALARM_PATTERN, new BeiAnBeiAlarmProtocol());
		protocolPool.put(ProtocolConstants.BEIANBEI_RECORD_PATTERN, new BeiAnBeiRecordProtocol());
		protocolPool.put(ProtocolConstants.NURSE_LINK_PATTERN, new NurseLinkProtocol());
		protocolPool.put(ProtocolConstants.NURSE_MULTILBS_PATTERN, new NurseMultiLBSProtocol());
		protocolPool.put(ProtocolConstants.NURSE_LBS_PATTERN, new NurseLBSProtocol());
		protocolPool.put(ProtocolConstants.NURSE_GPS_PATTERN, new NurseGPSProtocol());
	}
	
	/**
	 * 根据消息，用正则匹配然后选择协议
	 * @param msg
	 * @return
	 */
	public BaseProtocol chooseProtocol(String msg){
		
		Set<Entry<String,BaseProtocol>> entrySet = protocolPool.entrySet();
		Iterator<Entry<String,BaseProtocol>> iter = entrySet.iterator();
		Entry<String,BaseProtocol> entry;
		String pattern ;
		BaseProtocol baseProtocol;
		while(iter.hasNext()){
			entry = iter.next();
			pattern = entry.getKey();
			if(Pattern.compile(pattern).matcher(msg).matches()){
				baseProtocol = entry.getValue();
				baseProtocol.pattern = pattern;
				return baseProtocol;
			}
		}
		
		return null;
	}
	
	 
}
